centos7安装mongodb3.2
通过yum安装mongodb
yum添加MongoDB存储库
CentOS的默认存储库中不存在mongodb-org包。但是,MongoDB维护一个专用的存储库,现在将其添加到服务器。使用vi编辑器为yum创建.repo文件,yum是CentOS的软件包管理实用程序:1
2
3
4
5
6
7
8
9
10[root@localhost etc]# vi /etc/yum.repos.d/mongodb-org.repo
把以下内容加入mongodb-org.repo文件中
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
然后保存关闭文件
在继续之前,首先验证MongoDB存储库是否存在于yum实用程序中。 repolist命令显示已启用的存储库的列表:1
2
3
4
5
6
7
8
9
10
11[root@localhost etc]# yum repolist
输出结果:
Output
. . .
repo id                          repo name
base/7/x86_64                    CentOS-7 - Base
extras/7/x86_64                  CentOS-7 - Extras
mongodb-org-3.2/7/x86_64         MongoDB Repository
updates/7/x86_64                 CentOS-7 - Updates
. . .
可以看见mongodb-org-3.2/7/x86_64,证明mongodb已经在yum中了
yum安装MongoDB
| 1 | [root@localhost etc]# yum install mongodb-org | 
在每次提示时,键入Y,然后按ENTER键。安装后,查看mongodb 版本1
2
3
4[root@localhost etc]# mongod -version
输出结果:
db version v3.2.12
安装后使用systemctl实用工具启动MongoDB服务:1
[root@localhost etc]# systemctl reload mongod
在执行start命令后,systemctl实用程序不提供结果,但是我们可以通过使用tail命令查看mongod.log文件的结尾,以此来检查是否启动服务:1
2
3
4tail /var/log/mongodb/mongod.log
Output
. . .
[initandlisten] waiting for connections on port 27017
证明mongod服务已启动,客户端就可以链接mongod服务了
客户端链接mongod服务
| 1 | [root@localhost ~]# mongo | 
注意:启动MongoDB Shell时,用户可能将看到如下警告:1
** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
MongoDB是一个进程级应用程序,其可以通过新建其他进程来帮其分担其工作量。为了使MongoDB保持其最高效的状态,会有一个警告,其启动的进程数量应该是在任何给定时间可以打开文件数量的一半。要解决此警告,编辑20-nproc.conf文件更改mongod的过程soft rlimit值:1
[root@localhost ~]# vi /etc/security/limits.d/20-nproc.conf
将以下行添加到文件末尾:1
2. . .
mongod soft nproc 32000
保存并退出。要使用MongoDB的新限制,则使用systemctl实用程序将其重新启动:1
[root@localhost ~]# systemctl restart mongod
重新链接mongod服务1
2
3
4
5
6
7
8
9
10
11
12
13[root@localhost ~]# mongo
输出结果:
MongoDB shell version: 3.2.12
connecting to: test
Server has startup warnings: 
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] 
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'ne
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] 
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'ne
2017-03-29T12:04:53.493+0800 I CONTROL  [initandlisten]
使用mongo的帮助命令1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24> help或db.help()
输出结果:
    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                   administrative help
    help connect                 connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce
    show dbs                     show database names
    show collections             show collections in current database
    show users                   show users in current database
    show profile                 show most recent system.profile entries with time >= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, 'global' is default
    use <db_name>                set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )     list objects in foo where a == 1
    it                           result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x   set default number of items to display on shell
    exit                         quit the mongo shell
查看有哪些数据库,默认有一个local库1
2
3
4
5
6
7
8
9
10
11> show dbs
local  0.000GB
> use yytest # 有就使用这个数据库,没有就创建一个新的
> db.yytest.insert({"name":"yy","age":10}) # 向yytest数据库插入一条记录
输出结果:
WriteResult({ "nInserted" : 1 })
> db.yytest.find() # 查看本数据库所有记录
输出结果:
{ "_id" : ObjectId("58db8e87c1a8030da155a084"), "name" : "yy", "age" : 10 }
退出服务连接:1
2
3> exit
输出结果:
Bye
mongodb用户权限设置
注意: MongoDB 3.2 安全权限访问控制,在添加用户上面3.2版本和之前的版本有很大的区别,这里就说明下3.2的添加用户的方法
mongodb默认是没有访问权限控制的,需要在配置文件mongod.conf加配置
首先编辑mongod.conf1
[root@localhost etc]# vim /etc/mongod.conf
然后添加信息:1
2security:
  authorization:enabled
之后重启服务1
[root@localhost etc]# systemctl restart mongod.service
在安装MongoDB之后,第一次进入查看数据库,只有一个local库,admin库是不存在的1
2
3
41. MongoDB是没有默认管理员账号,所以要先添加管理员账号,再开启权限认证。
2. 切换到admin数据库,添加的账号才是管理员账号。
3. 用户只能在用户所在数据库登录,包括管理员账号。
4. 管理员可以管理所有数据库,但是不能直接管理其他数据库,要先在admin数据库认证后才可以。
所以要先创建admin数据库1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27> use admin
switched to db admin
> db.createUser({user:"admin",pwd:"gome",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
输出结果:
Successfully added user: {
    "user" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
> show users # 查看本数据库的用户
{
    "_id" : "admin.admin",
    "user" : "admin",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
> db.system.users.find() # xxxxx
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "9zLNi3zT6/AO5g==", "storedKey" : "SHlUm0VD43/UvAShi8BFnreujRE=", "serverKey" : "LLphzUajQGBvSFr4userAdminAnyDatabase", "db" : "admin" } ] }
验证添加的访问权限控制是否生效
创建一个yytest数据库1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36> use yytest
switched to db yytest
> db.yytest.insert({"name":"yy","age":10})
WriteResult({
    "writeError" : {
        "code" : 13,
        "errmsg" : "not authorized on yytest to execute command { insert: \"yytest\", documents: [ { _id: ObjectId('58db8dbdc1a8030da155a081'), name: \"yy\", age: 10.0 } ], ordered: true }"
    }
})
提示说没有权限
为yytest数据库创建用户
> db.createUser({user:"user",pwd:"gome",roles:[{role:"readWrite",db:"yytest"}]})
Successfully added user: {
    "user" : "user",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "yytest"
        }
    ]
}
> db.yytest.insert({"name":"yy","age":10})
WriteResult({
    "writeError" : {
        "code" : 13,
        "errmsg" : "not authorized on yytest to execute command { insert: \"yytest\", documents: [ { _id: ObjectId('58db8e5dc1a8030da155a083'), name: \"yy\", age: 10.0 } ], ordered: true }"
    }
})
提示说没有权限
> db.auth("user","gome") # 类似登录
> db.yytest.insert({"name":"yy","age":10})
WriteResult({ "nInserted" : 1 })
发现操作成功了,也就是通过权限验证了
到这,设置用户访问权限控制就完成了
更多的操作还需要自己动手多多实践···
 
         
              